<template>
	<div>
		<el-dialog :title="title" :close-on-click-modal="false" v-dialogDrag :visible.sync="visible">
			<el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading"
				:class="method==='view'?'readonly':''" :disabled="method==='view'" label-width="120px">
				<el-row :gutter="15">
					<el-col :span="12">
						<el-form-item label="固定资产" prop="fixed.id" :rules="[]">
							<GridSelect title="选择固定资产" labelName='assetsName' labelValue='id'
								:value="inputForm.fixed.id" :limit="1" @getValue='(value) => {inputForm.fixed.id=value}'
								:columns="[
								{
								prop: 'id',
								label: '主键'
								}
								]" :searchs="[
								{
								prop: 'assetsName',
								label: '资产名称'
								}
								]" dataListUrl="/dlyrl.oa/fixedassets/fixedAssets/list" entityBeanName="fixedAssets"
								queryEntityUrl="/dlyrl.oa/fixedassets/fixedAssets/queryById">
							</GridSelect>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="故障图片" prop="faultpicture" :rules="[]">
							<el-upload ref="faultpicture" v-if="visible" list-type="picture-card"
								:action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl.oa/fixedrepair/fixedRepair`"
								:headers="{token: $cookie.get('token')}" :on-preview="(file, fileList) => {
									$alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
									dangerouslyUseHTMLString: true,
									showConfirmButton: false,
									closeOnClickModal: true,
									customClass: 'showPic'
									});
								}" :on-success="(response, file, fileList) => {
								inputForm.faultpicture = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
								}" :on-remove="(file, fileList) => {
								$http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
									$message.success(data.msg)
								})
								inputForm.faultpicture = fileList.map(item => item.url).join('|')
								}" :before-remove="(file, fileList) => {
								return $confirm(`确定移除 ${file.name}？`)
								}" multiple :limit="5" :on-exceed="(files, fileList) =>{
								$message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
								}" :file-list="faultpictureArra">
								<i class="el-icon-plus"></i>
							</el-upload>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="故障描述" prop="faultcontent" :rules="[]">
							<el-input type="textarea" v-model="inputForm.faultcontent" placeholder="请填写故障描述"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-form-item label="修复图片" prop="repairpicture" :rules="[]">
							<el-upload ref="repairpicture" v-if="visible" list-type="picture-card"
								:action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl.oa/fixedrepair/fixedRepair`"
								:headers="{token: $cookie.get('token')}" :on-preview="(file, fileList) => {
									$alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
									dangerouslyUseHTMLString: true,
									showConfirmButton: false,
									closeOnClickModal: true,
									customClass: 'showPic'
									});
								}" :on-success="(response, file, fileList) => {
								inputForm.repairpicture = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
								}" :on-remove="(file, fileList) => {
								$http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
									$message.success(data.msg)
								})
								inputForm.repairpicture = fileList.map(item => item.url).join('|')
								}" :before-remove="(file, fileList) => {
								return $confirm(`确定移除 ${file.name}？`)
								}" multiple :limit="5" :on-exceed="(files, fileList) =>{
								$message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
								}" :file-list="repairpictureArra">
								<i class="el-icon-plus"></i>
							</el-upload>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="修复描述" prop="repaircontent" :rules="[]">
							<el-input type="textarea" v-model="inputForm.repaircontent" placeholder="请填写修复描述">
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="是否修复" prop="isrepair" :rules="[]">
							<el-select v-model="inputForm.isrepair" placeholder="请选择" style="width: 100%;">
								<el-option v-for="item in $dictUtils.getDictList('yes_no')" :key="item.value"
									:label="item.label" :value="item.value">
								</el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="备注信息" prop="remarks" :rules="[]">
							<el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button size="small" @click="visible = false">关闭</el-button>
				<el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定
				</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import GridSelect from '@/components/gridSelect'
	export default {
		data() {
			return {
				title: '',
				method: '',
				visible: false,
				loading: false,
				faultpictureArra: [],
				repairpictureArra: [],
				inputForm: {
					id: '',
					fixed: {
						id: ''
					},
					faultpicture: '',
					faultcontent: '',
					repairpicture: '',
					repaircontent: '',
					isrepair: '',
					remarks: ''
				}
			}
		},
		components: {
			GridSelect
		},
		methods: {
			init(method, id) {
				this.method = method
				this.inputForm.id = id
				if (method === 'add') {
					this.title = `新建固定资产报修`
				} else if (method === 'edit') {
					this.title = '修改固定资产报修'
				} else if (method === 'view') {
					this.title = '查看固定资产报修'
				}
				this.faultpictureArra = []
				this.repairpictureArra = []
				this.visible = true
				this.loading = false
				this.$nextTick(() => {
					this.$refs.inputForm.resetFields()
					if (method === 'edit' || method === 'view') { // 修改或者查看
						this.loading = true
						this.$http({
							url: `/dlyrl/oa/fixedrepair/fixedRepair/queryById?id=${this.inputForm.id}`,
							method: 'get'
						}).then(({
							data
						}) => {
							this.inputForm = this.recover(this.inputForm, data.fixedRepair)
							this.inputForm.faultpicture.split('|').forEach((item) => {
								if (item.trim().length > 0) {
									this.faultpictureArra.push({
										name: decodeURIComponent(item.substring(item
											.lastIndexOf('/') + 1)),
										url: item
									})
								}
							})
							this.inputForm.repairpicture.split('|').forEach((item) => {
								if (item.trim().length > 0) {
									this.repairpictureArra.push({
										name: decodeURIComponent(item.substring(item
											.lastIndexOf('/') + 1)),
										url: item
									})
								}
							})
							this.loading = false
						})
					}
				})
			},
			// 表单提交
			doSubmit() {
				this.$refs['inputForm'].validate((valid) => {
					if (valid) {
						this.loading = true
						this.$http({
							url: `/dlyrl/oa/fixedrepair/fixedRepair/save`,
							method: 'post',
							data: this.inputForm
						}).then(({
							data
						}) => {
							this.loading = false
							if (data && data.success) {
								this.visible = false
								this.$message.success(data.msg)
								this.$emit('refreshDataList')
							}
						})
					}
				})
			}
		}
	}
</script>
